<?php
// +----------------------------------------------------------------------
// | 商家模块
// +----------------------------------------------------------------------

namespace app\mall\model;

use think\Model as ThinkModel;
use util\Tree;
use think\facade\Db;

/**
 * 分类模型
 * @package app\mall\model
 */
class Category extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mall_category';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    /**
     * 获取分类列表
     * @return array|mixed
     */
    public static function getList()
    {
        $data_list = cache('mall_category_list');
        if (!$data_list) {
            $data_list = self::where('status', 1)->where('pid', 0)->order('sort')->field('id, title,icon')->select();
			foreach($data_list as $key=>$value){
				$data_list[$key]['logo'] = get_file_path($value['icon']);
				$data_list[$key]['url'] = real_url('mall/goods/index', ['id' => $value['id']]);
			}
            // 非开发模式，缓存数据
            if (config('app.develop_mode') == 0) {
                cache('mall_category_list', $data_list);
            }
        }
        return $data_list;
    }
	
	/**
	 * 获取简单分类列表
	 * @return array|mixed
	 */
	public static function getColumn()
	{
	    $data_list = cache('mall_category_column');
	    if (!$data_list) {
	        $data_list = self::where('status', 1)->where('pid', 0)->order('sort')->column('title', 'id');
	        // 非开发模式，缓存数据
	        if (config('app.develop_mode') == 0) {
	            cache('mall_category_column', $data_list);
	        }
	    }
	    return $data_list;
	}
	/**
	 * 获取树状栏目
	 * @param int $id 需要隐藏的栏目id
	 * @param string $default 默认第一个节点项，默认为“顶级栏目”，如果为false则不显示，也可传入其他名称
	 * @author PaulXun <paulxun@qq.com>
	 * @return array|mixed
	 */
	public static function getTreeList($id = 0, $default = '')
	{
	    $result[0] = '顶级栏目';
	
	    // 排除指定节点及其子节点
	    $where = [
	        ['status', '=', 1]
	    ];
	    if ($id !== 0) {
	        $hide_ids = array_merge([$id], self::getChildsId($id));
	        $where[]  = ['id', 'not in', $hide_ids];
	    }
	
	    $data_list = Tree::toList(self::where($where)->order('pid,id')->column('id,pid,title'));
	    foreach ($data_list as $item) {
	        $result[$item['id']] = $item['title_display'];
	    }
	
	    // 设置默认节点项标题
	    if ($default != '') {
	        $result[0] = $default;
	    }
	
	    // 隐藏默认节点项
	    if ($default === false) {
	        unset($result[0]);
	    }
	
	    return $result;
	}
	/**
	 * 获取所有子栏目id
	 * @param int $pid 父级id
	 * @author PaulXun <paulxun@qq.com>
	 * @return array
	 */
	public static function getChildsId($pid = 0)
	{
	    $ids = self::where('pid', $pid)->column('id');
	    foreach ($ids as $value) {
	        $ids = array_merge($ids, self::getChildsId($value));
	    }
	    return $ids;
	}
    /**
     * 获取指定类型数据
     * @param int $cid 类型id
     * @return mixed|static
     */
    public static function getInfo($cid = 0)
    {
        $result = cache('mall_category_info_'. $cid);
        if (!$result) {
            $result = self::where('id', $cid)->find();
            // 非开发模式，缓存数据
            if (config('app.develop_mode') == 0) {
                cache('mall_category_info_'. $cid, $result);
            }
        }
        return $result;
    }
}